# encoding=utf8

import pymysql,requests,urllib3,datetime,re
from lxml import etree
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

host = '127.0.0.1'
users = 'root'
pwd = '123123'
port = 3306
dbs = 'ans'


def mysql(名字,年龄,年代,性别,国籍,标签,职业,专业热度,关注热度,预测热度,带货力,商业价值):
    value = ((名字,年龄,年代,性别,国籍,标签,职业,专业热度,关注热度,预测热度,带货力,商业价值))
    db = pymysql.connect(host=host, user=users,passwd=pwd,port=port, db=dbs)
    cursor = db.cursor()
    sql = "INSERT INTO 艺人数据(名字,年龄,年代,性别,国籍,标签,职业,专业热度,关注热度,预测热度,带货力,商业价值) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    try:
        cursor.execute(sql, value)
        db.commit()
        print('success!')
    except Exception as e:
        db.rollback()
        print("error.", e)
    db.close()

def join_str(sth):
    str = ''.join(sth).strip()
    return str

def downtext(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Mobile Safari/537.36 Edg/103.0.1264.37',
        'Cookie':'JSESSIONID=7e08860b-d58f-47c6-aa63-da76db49b2c6; gr_user_id=ba7ec484-099c-4410-8913-808ff4c47b89; a92bf18a95cb2b3c_gr_session_id=a7e4f8cc-ddb9-4b4b-9679-a662252fc842; a92bf18a95cb2b3c_gr_session_id_a7e4f8cc-ddb9-4b4b-9679-a662252fc842=true; Hm_lvt_3dd500c95a0505fd002b19136f7a4543=1656425176; Hm_lpvt_3dd500c95a0505fd002b19136f7a4543=1656425746',
    }
    for i in range(1,432):
        params = {
            'pageindex' : i,
            'pagesize' : 10,
        }
    res = requests.get(url=url,headers=headers,params=params).text
    # res = re.findall('{"ClusterId": (.*?), "T',res)
    # print(res)
    return res
def downjson(url,page):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Mobile Safari/537.36 Edg/103.0.1264.37',
        'Cookie':'JSESSIONID=7e08860b-d58f-47c6-aa63-da76db49b2c6; gr_user_id=ba7ec484-099c-4410-8913-808ff4c47b89; a92bf18a95cb2b3c_gr_session_id=a7e4f8cc-ddb9-4b4b-9679-a662252fc842; a92bf18a95cb2b3c_gr_session_id_a7e4f8cc-ddb9-4b4b-9679-a662252fc842=true; Hm_lvt_3dd500c95a0505fd002b19136f7a4543=1656425176; Hm_lpvt_3dd500c95a0505fd002b19136f7a4543=1656425746',

    }
    # for i in range(1,432):
    params = {
        'pageindex' : page,
        'pagesize' : 10,
    }
    res = requests.get(url=url,headers=headers,params=params).json()
    # print(res)
    # print(type(res))
    return res
def func1(res):
    try:
        for table in res:
            名字 = join_str(table['StarBaseName'])
            年龄 = table['StarAge']
            年代 = join_str(table['BirthYear'])
            性别 = join_str(table['StarSex'])
            国籍 = join_str(table['Country'])
            try:
                标签 = join_str(table['StarCustomTags'])
            except:
                continue
            职业 = join_str(table['Professions'])
            专业热度 = table['MajorHotIndex_L2']
            关注热度 = table['FocusHotIndex_L2']
            预测热度 = table['PredictHotIndex_L2']
            带货力 = table['TakeGoodsIndex_L2']
            商业价值 = table['FlowValueIndex_L1']
            mysql(名字, 年龄, 年代, 性别, 国籍, 标签, 职业, 专业热度, 关注热度, 预测热度, 带货力, 商业价值)
    except:
        print('跳过一个')
    return 0
def func(res):
    # print(res)
    data = res['data']
    tables = data['table0']
    print(tables)
    for table in tables:
        try:
            名字 = join_str(table['StarBaseName'])
            年龄 = table['StarAge']
            年代 = join_str(table['BirthYear'])
            性别 = join_str(table['StarSex'])
            国籍 = join_str(table['Country'])
            try:
                标签 = join_str(table['StarCustomTags'])
            except:
                continue
            职业 = join_str(table['Professions'])
            专业热度 = table['MajorHotIndex_L2']
            关注热度 = table['FocusHotIndex_L2']
            预测热度 = table['PredictHotIndex_L2']
            带货力 = table['TakeGoodsIndex_L2']
            商业价值 = table['FlowValueIndex_L1']
            mysql(名字, 年龄, 年代, 性别, 国籍, 标签, 职业, 专业热度, 关注热度, 预测热度, 带货力, 商业价值)
        except:
            func1(table)
        print(名字)



    return 0
if __name__ == "__main__":
    for page in range(1,432):
    #     print('page',page)
        url = 'https://star.endata.com.cn/api/star/search'
        # print(url)
        res = downjson(url,page)
        # print(res)
        func(res)
        # break

